home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 May
/
EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso
/
softwareupdate
/
system
/
amigados
/
files
/
functions.doc
< prev
next >
Wrap
Text File
|
1996-10-10
|
21KB
|
597 lines
2 FILES - FUNCTIONS
2.1 QUICK REFERENCE
Here is a complete list of all file functions described in this
chapter:
Function Description
-------------------------------------------------------------
Close() Closes a previously opened file.
Lock() Puts a shared or excusive lock on an object.
Open() Opens a new or old file for IO.
OpenFromLock() Opens an already locked file.
Read() Reads a specified number of bytes in a file.
Seek() Moves the file cursor inside a file.
UnLock() Unlocks a previously locked object.
Write() Writes a specified number of bytes to a file.
-------------------------------------------------------------
---------------------------------------------------------------
Close()
ROM library: "dos.library/Close", (All Versions)
#include <clib/dos_protos.h>
Closes a previously opened file.
Synopsis: ok = Close( file );
ok: (LONG) If the file could successfully be saved TRUE
is returned. On the other hand, if there was some
sort of problem FALSE is returned.
Even if the file could not be closed successfully
will the file cursor and all memory which have
previously been allocated internally now been
removed and deallocated. So even if you could not
close the file successfully you should NOT try to
close it again (that would crash the system)!
If the file could not be closed there is actually
very little we can do about it. We might want to
resave some data (in another file) if we are afraid
the data otherwise might be lost, but we can not
do anything about the actual error. Just remember
that you must not try to close the file again, nor
to use it any more!
On the old systems prior to Release 2 (older than
V36) the Close() do not return anything.
file: (BPTR) A BCPL pointer to an already open file which
should now be cosed.
Remember to close ALL files you have opened!
Here is a simple example on how to use the Close() function:
- - -
/* Close the file: */
if( Close( my_file ) )
printf( "File closed!\n" );
else
printf( "Error! File could not be closed!\n" );
/* Remember that even if the file could not be */
/* closed we must NOT try to close it again! */
See also: IoErr(), Lock(), Open(), OpenFromLock(), Read(),
Seek(), UnLock(), Write()
---------------------------------------------------------------
Lock()
ROM library: "dos.library/Lock", (All Versions)
#include <clib/dos_protos.h>
Puts a shared or excusive lock on an object (e.g. file).
Synopsis: lock = Lock( file_name, mode );
lock: (BPTR) If the file could be locked a BPTR (BPCL
pointer) is returned. If the file on the other
hand could not be locked NULL is returned.
Possible reasons why the lock failed:
1. The file simply does not exist.
2. Some other program has an exclusive lock on
the file, and consequntly no others may lock
it (both shared and exclusive lock requests
will fail).
3. Som other program has a shared lock on the
file but you wanted an exclusive lock. (If
the file has a shared lock on it, and you
tries to add another shared lock on it there
will not be any problems.)
file_name: (STRPTR) Pointer to a text string which contains
the name of the file you want to lock. If the file
is not located in the current directory you have
to include the necessary path.
mode: (LONG) On the Amiga there exist two types of
locks: (The flags are defined in header file
"dos/dos.h")
SHARED_LOCK: Other programs may read data in
the file, but no one else may
alter the file. (You can also
use the flag "ACCESS_READ"
which does the same thing.)
EXCLUSIVE_LOCK: No other programs may use this
file. (You can also use the
flag "ACCESS_WRITE" which does
the same thing.)
Here is a simple example on how to use the Lock() function:
/* A "BCPL" pointer to our lock: */
BPTR my_lock;
- - -
/* Try to lock the file with an exclusive lock: */
my_lock = Lock( "Highscore.dat", EXCLUSIVE_LOCK );
/* Check if we have successfully locked the file or not: */
if( my_lock == NULL )
printf( "Could not lock the file!\n" );
See also: Close(), IoErr(), Open(), OpenFromLock(), Read(),
Seek(), UnLock(), Write()
---------------------------------------------------------------
Open()
ROM library: "dos.library/Open", (All Versions)
#include <clib/dos_protos.h>
Opens a new or old file for input and/or output, and puts a
read" or "write" lock on the file.
Synopsis: my_file = Open( file_name, mode );
my_file: (BPTR) If the file could be opened a BPTR (BPCL
pointer) is returned. If the file on the other
hand could not be opened NULL is returned.
file_name: (STRPTR) Pointer to a text string which contains
the name of the file you want to open. If the file
is not located in the current directory you have
to include any necessary device/directory names.
(The device/directory part is usually reffered as
the "path", and tells AmigaDOS where the file can
be found.)
mode: (LONG) When you want to open a file you must tell
AmigaDOS how you want to open the file. For the
mement there exist three modes: (The flags are
defined in header file "dos/dos.h")
MODE_OLDFILE: Opens an existing file for reading
and writing. The file will be
"locked" so other programs can read
it but not modify it before you
have "un-locked" the file. This is
usually called a "write lock"
("shared lock").
If the file does not exist or it
is used by some other program
("read or write locked") Open()
will fail and NULL is returned.
MODE_NEWFILE: Opens a new file for writing. If
the file already exist it will be
deleted so a new file with the
same name can be created.
Note that the data in an existing
file will be deleted if you use
this flag on an already existing
file! Be careful so you do not
destroy important data!
The file will be "locked" so no
other programs can read nor write
to it. This is usually called a
"read/write lock" ("exclusive
lock")
If the file already exist and is
used by some other program ("read
or write locked") Open() will fail
and NULL is returned.
MODE_READWRITE: Opens an old file as described
with the "MODE_OLDFILE" flag.
However, if the file does not
exist it will be created.
If the file is used by some other
program ("read or write locked")
Open() will fail and NULL is
returned.
Regardless of what mode you chose the file cursor
will always be positioned in the beginning of the
file if it could be opened. (The file cursor will
point to the first byte/character in the file).
You may of course have as many files open at the same time as
you want. Note that each file you have opened has its own file
cursor. When you work with one file you do not have to bother
about the other ones. If you move around inside one file the
others files' file cursors will remain unmodified. Of course
you need a BPTR for each file, and you have to open each file
separately.
Here is a simple example on how to use the Open() function:
/* A "BCPL" pointer to our file: */
BPTR my_file;
- - -
/* Try to open a file as "new": */
my_file = Open( "Highscore.dat", MODE_NEWFILE );
/* Check if we have successfully opened the file or not: */
if( my_file == NULL )
printf( "Could not open the file!\n" );
See also: Close(), IoErr(), Lock(), OpenFromLock(), Read(),
Seek(), UnLock(), Write()
---------------------------------------------------------------
OpenFromLock()
ROM library: "dos.library/OpenFromLock", (V36+)
#include <clib/dos_protos.h>
Opens an already locked file.
Synopsis: my_file = OpenFromLock( lock );
my_file: (BPTR) If the file could be opened a BPTR (BPCL
pointer) is returned. If the file on the other hand
could not be opened NULL is returned.
lock: (BPTR) A BPCL pointer to a lock which file should be
opened. You must of course have locked the file
successfully before you may open the file with it!
Here is a simple example on how to use the OpenFromLock()
function:
/* A "BCPL" pointer to our lock: */
BPTR my_lock;
/* A "BCPL" pointer to our file: */
BPTR my_file;
- - -
/* Put an exclusive lock on an existing file: */
my_lock = Lock( "RAM:HighScore.dat", EXCLUSIVE_LOCK );
/* Could we lock the file successfully? */
if( !my_lock )
{
/* Problems! Inform the user: */
printf( "Could not put an exclusive lock on the file!\n" );
/* Exit with an error code: */
exit( 20 );
}
/* We will now try to open the file with help */
/* of the lock we already have: */
my_file = OpenFromLock( my_lock );
/* Have we opened the file successfully? */
if( !my_file )
{
/* Problems! Inform the user: */
printf( "Error! Could not open the file!\n" );
/* Unlock the file: */
UnLock( my_lock );
/* Exit with an error code: */
exit( 21 );
}
- - -
/* Close the file: */
Close( my_file );
/* Unlock the file: */
UnLock( my_lock );
See also: Close(), IoErr(), Lock(), Open(), Read(), Seek(),
UnLock(), Write()
---------------------------------------------------------------
Read()
ROM library: "dos.library/Read", (All Versions)
#include <clib/dos_protos.h>
Reads a specified number of bytes in a file.
Synopsis: count = Read( file, buffer, size );
count: (LONG) The function returns the number of bytes
actually read. When you tell AmigaDOS that you want
to read X number of bytes it is not certain that you
actually can do it. The file might be shorter than
you expected or the file might be corrupted, etc...
The returned value can be:
1. equal to the number of bytes requested. In this
case everything went as expected. The requested
number of bytes were read, and there were no
errors.
2. equal to -1. In this case there was an error
while the function tried to read the data. How
much, if any, data were actually read before the
error occured is imposible to say, and you should
not use the values in the buffer. If you receive
this value you may call IoErr() to get more
information about the error.
3. less than the number of bytes requested, but
not equal to -1. In this case we have simply
reached the end of the file, and there was no
error.
Please note that reaching the end of the file
before expected can sometimes indicate that there
was some sort of error. If you have saved for
example 10 structures of a certain size, and when
you later tries to read the file and only get four
structures there is obviously something wrong. It
is, however, not a dos error and the file itself
is ok. It is the data in the file which is
incorrect, but that is not AmigaDOS's fault.
file: (BPTR) A BCPL pointer to an already open file.
buffer: (APTR) Pointer to some memory where the collected
data can be stored.
size: (LONG) Number of bytes you want to read.
Note! The function does not check that the buffer you give it
is big enough to store all values you requested! You may never
try to read more bytes than can fit in your buffer!
Here is a simple example on how to use the Read() function:
/* Some memory where we will store the collected data: */
int my_highscore[ 10 ];
/* The number of bytes actually collected: */
long bytes_read;
- - -
/* Read some data from an already open file: */
bytes_read = Read( my_file, my_highscore, sizeof( my_highscore ) );
/* Did we get all data? */
if( bytes_read != sizeof( my_highscore ) )
{
/* No! We could not read all values! */
printf( "Error! Could read all values!\n" );
}
else
{
/* Yes! All recuested data were successfully collected! */
printf( "Data collected!\n" );
}
See also: Close(), IoErr(), Lock(), Open(), OpenFromLock(),
Seek(), UnLock(), Write()
---------------------------------------------------------------
Seek()
ROM library: "dos.library/Seek", (All Versions)
#include <clib/dos_protos.h>
Moves the file cursor to a specified position in a file.
Synopsis: old_pos = Seek( file, position, offset );
old_pos: (LONG) The function returns the previous position
in the file, or -1 if there was an error and the
file cursor could not be moved. If there was an
error you should of course not try to read or
write any data until the problem has been solved
and you have successfully positioned the file
cursor.
If there is an error you should call IoErr() to
get more information about the error.
file: (BPTR) A BCPL pointer to an already open file.
position: (LONG) The position you want to move the file
cursor to. Note that this value is an relative
to the specified "offset position".
offest: (LONG) The specified position is relative to one
one of these "offset positions": (The flags are
defined in header file "dos/dos.h")
OFFSET_BEGINNING: Beginning of the file.
OFFSET_CURRENT: Current position.
OFFSET_END: The end of the file.
If you want to move the file cursor to the beginning of the
file (pointing to the first byte) you set the offset position
to "OFFSET_BEGINNING" and the position value to 0.
If you on the other hand want to move the file cursor to the
end of the file you set the offset position to "OFFSET_END" and
the position value to 0.
To move 10 bytes forward from the current position you set the
offset position to "OFFSET_CURRENT" and the position value to
10. To move 10 bytes backwards you set the position value to
-10.
Here are some simple examples on how to use the Seek() function:
/* Move the file cursor to the end of the file: */
Seek( my_file, 0, OFFSET_END );
/* Move the file cursor to the beginning of the file: */
Seek( my_file, 0, OFFSET_BEGINNING );
/* Move the file cursor 10 bytes forward */
/* from the current position: */
Seek( my_file, 10, OFFSET_CURRENT );
/* Move the file cursor 10 bytes backwards */
/* from the current position: */
Seek( my_file, -10, OFFSET_CURRENT );
See also: Close(), IoErr(), Lock(), Open(), OpenFromLock(),
Read(), UnLock(), Write()
---------------------------------------------------------------
UnLock()
ROM library: "dos.library/UnLock", (All Versions)
#include <clib/dos_protos.h>
Unlocks a previously locked object (e.g. file).
Synopsis: UnLock( lock );
lock: (BPTR) A BPCL pointer to a lock that should be
unlocked. Remember that every file that you have
locked must be unlocked when you do not need the
look any more!
Here is a simple example on how to use the UnLock() function:
- - -
/* Unlock the file: */
UnLock( my_lock );
See also: Close(), IoErr(), Lock(), Open(), OpenFromLock(),
Read(), Seek(), Write()
---------------------------------------------------------------
Write()
ROM library: "dos.library/Write", (All Versions)
#include <clib/dos_protos.h>
Writes a specified number of bytes to a file.
Synopsis: count = Write( file, buffer, size );
count: (LONG) The function returns the number of bytes
actually written. When you tell AmigaDOS that you want
to write X number of bytes it is not certain that you
actually can do it. The file is maybe write protected,
or the disk is full etc...
The returned value can be:
1. equal to the number of bytes requested. In this
case everything went as expected. The requested
number of bytes were written, and there were no
errors.
2. equal to -1. In this case there was an error
while the function tried to write the data. No
data will have been saved. If you receive this
value you may call IoErr() to get more information
about the error.
3. less than the number of bytes requested, but
not equal to -1. In this case the operation was
aborted because of some external event (disk full
etc...), and the value returned tells us how many
bytes that have been saved.
If you could not save all the data you should
in most situations remove the half filled file
(it is probably useless if not all data could
be saved) and ask the user to for example insert
a new disk etc... depending on what have happened.
Do NOT just tell the user that there was a problem
and then quit! (In small examples and if the data
is unimportant this is of course OK, but bigger
applications should try to solve the problem.)
file: (BPTR) A BCPL pointer to an already open file.
buffer: (APTR) Pointer to some memory where the data that
should be written (saved) are stored.
size: (LONG) Number of bytes you want to write.
Here is a simple example on how to use the Write() function:
/* Some data we want to save: */
int my_highscore[ 10 ] =
{ 34, 76, 328, 723, 1027, 1201, 1416, 2645, 3050, 3876 };
/* The number of bytes actually written: */
long bytes_written;
- - -
/* Write some data to a file: */
bytes_written = Write( my_file, my_highscore, sizeof( my_highscore ) );
/* Did we write all data? */
if( bytes_written != sizeof( my_highscore ) )
{
/* No! We could not write (save) all data! */
printf( "Error! Could not save all values!\n" );
}
else
{
/* Yes, all numbers have been written to the file! */
printf( "All values were saved successfully!\n" );
}
See also: Close(), IoErr(), Lock(), Open(), OpenFromLock(),
Read(), Seek(), UnLock()
---------------------------------------------------------------